package jp.co.showbiz.admin.talent.imp;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import jp.co.showbiz.admin.talent.dao.TalentDao;
import jp.co.showbiz.common.base.BaseBean;
import jp.co.showbiz.common.base.BaseException;
import jp.co.showbiz.common.entity.TalentBunyaMstBean;

public class TalentImpl {
	TalentDto bean;
		
	HashMap<String, String> globalMap;
	
	public TalentDto getBean() {
		return bean;
	}

	public void setBean(TalentDto bean) {
		this.bean = bean;
	}
	
	public TalentImpl() {
		super();
	}
	
	public TalentImpl(HashMap<String, String> globalMap) throws Exception {
		super();
		this.globalMap = globalMap;
	}

	
	
	//////////////////////////////////////////////////////////////////////
	@SuppressWarnings("unchecked")
	public boolean talentInsert(TalentDto dto) throws Exception {
		TalentDao dao = new TalentDao();
		dao.insert(dto);
		HashMap<String, Object> map = (HashMap<String, Object>)dao.maxTalentId(dto);
		dto.setMaxTalentId(map);
		return true;
	}
	
	@SuppressWarnings("unchecked")
	public boolean talentContentInsertPI(TalentDto dto) throws Exception {
		TalentDao dao = new TalentDao();
		dao.talentContentInsert(dto);
		HashMap<String, Object> map = (HashMap<String, Object>)dao.maxContentId(dto);
		dto.setMaxContentId(map);
		
		filePrc(dto);
		dao.talentContentInsertName(dto);
		return true;
	}
	
	public boolean talentContentInsertTA(TalentDto dto) throws Exception {
		TalentDao dao = new TalentDao();
		dao.talentContentInsertTA(dto);
		return true;
	}
	
	@SuppressWarnings("unchecked")
	public boolean getTalentContent(TalentDto dto) throws Exception {
		TalentDao dao = new TalentDao();
		HashMap<String, Object> map = (HashMap<String, Object>)dao.getTalentContent(dto);
		dto.getTalentContent(map);
		return true;
	}
	
	public boolean talentContentUpdatePI(TalentDto dto) throws Exception {
		TalentDao dao = new TalentDao();
		if(!(dto.getVgaFile() == null && dto.getQvgaFile() == null)){
			filePrc(dto);
		}
		dao.talentContentUpdatePI(dto);
		return true;
	}
	
	public void createFoler(String path){
		File newFile = new File(path);
		if (!newFile.exists()) {
			newFile.mkdirs();
    	}
	}
    private boolean filePrc(TalentDto dto) {
    	String destPath = globalMap.get("path_base");
    	String webPath = globalMap.get("path_photo") + "/" + dto.getTalentId();
    	destPath = destPath + webPath;
    	createFoler(destPath);
    	destPath = destPath + "/";
    	webPath = webPath + "/";
    	String baseFileName = globalMap.get("name_talent") + dto.getContentId() + "_" + dto.getTalentId();
    	dto.setFileName(baseFileName);
    	
    	if(dto.getVgaFile() != null) {
    		String[] extension = dto.getVgaFileFileName().split("[.]");
    		String saveFileName = globalMap.get("vga_thumb") + baseFileName + "." + extension[1];
    		dto.setFileName(dto.getFileName() + "." + extension[1]);
    		fileMove(dto.getVgaFile(), destPath, saveFileName);
    		dto.setVgaFileFileName(saveFileName);
    		dto.setFilePath(webPath);
    	}
    	if(dto.getQvgaFile() != null) {
    		String[] extension = dto.getQvgaFileFileName().split("[.]");
    		String saveFileName = globalMap.get("qvga_thumb") + baseFileName + "." + extension[1];;
    		dto.setFileName(dto.getFileName() + "." + extension[1]);
    		fileMove(dto.getQvgaFile(), destPath, saveFileName);
    		dto.setQvgaFileFileName(saveFileName);
    		dto.setFilePath(webPath);
    	}
    	return true;
    }
    
    // Upload File
    private boolean fileMove(File f, String destPath, String fileName) {
    	// create folder
    	File folder = new File(destPath);
    	if(!folder.exists()){
    		folder.mkdirs();
    	}
    	File destfile = new File(new File(destPath), fileName);
    	if(destfile.exists()) {
    		destfile.delete();
    	}
    	if(f.renameTo(destfile)) {
    		return true;
    	}
		return false;
    }
	
    public boolean groupDelete(TalentDto dto) throws Exception {
    	TalentDao dao = new TalentDao();
    	dao.groupDelete(dto);
    	return true;
    }

    public boolean groupUpdate(TalentDto dto) throws Exception {
    	TalentDao dao = new TalentDao();
    	dao.groupUpdate(dto);
    	return true;
    }
	
	
	//////////////////////////////////////////////////////////////////////
	
	
	

	public String getTotalCount() throws Exception {
		TalentDao dao = new TalentDao();
		
		return dao.getTotalCount();
	}

	public String getSearchCount(BaseBean bBean) throws Exception {
		TalentDao dao = new TalentDao();
		
		return dao.getSearchCount(bBean);
	}	
	
	public String getTalentId() throws Exception {
		TalentDao dao = new TalentDao();
	
		return dao.getTalentId();
	}	
	
	public TalentDto getTalent(BaseBean bBean) throws Exception {
		TalentDao dao = new TalentDao();
		TalentDto dto = (TalentDto) dao.objSelect((Object)bBean);
		dto.setListTalent(dao.getListTalent(dto));
		dto.setListTalentGroup(dao.getListTalentGroup(dto));
		
		return dto;
	}
	
	public List<TalentDto> getTalentList() throws Exception{
		TalentDao dao = new TalentDao();
		List<TalentDto> bean = dao.selectList();
		
		return bean; 
	}
	
	public List<TalentDto> getTalentList(BaseBean bBean) throws Exception{
		TalentDao dao = new TalentDao();
		List<TalentDto> bean = dao.selectList(bBean);
		
		return bean; 
	}
	
	public List<TalentDto> getTalentListPage(BaseBean bBean, int skip, int maxSize) throws Exception{
		TalentDao dao = new TalentDao();
		List<TalentDto> bean = dao.selectListPage(bBean, skip, maxSize);
		
		return bean; 
	}
	
	public List<TalentDto> getTalentSearchList(BaseBean bBean) throws Exception{
		TalentDao dao = new TalentDao();
		List<TalentDto> bean = dao.selectSearchList(bBean);
		
		return bean; 
	}
	
	public List<TalentDto> getTalentSearchListPage(BaseBean bBean, int skip, int maxSize) throws Exception{
		TalentDao dao = new TalentDao();
		List<TalentDto> bean = dao.selectSearchListPage(bBean, skip, maxSize);
		
		return bean; 
	}
	
	public List<TalentBunyaMstBean> getTalentBunyaList(BaseBean bBean) throws Exception {
		TalentDao dao = new TalentDao();
		List<TalentBunyaMstBean> bean = dao.selectBunya(bBean);
		
		return bean;
	}

	public List<TalentPickupDto> getPickupList() throws Exception {
		// TODO Auto-generated method stub
		TalentDao dao = new TalentDao();
		return dao.getPickList();
	}

	public void regTalentPickup(String ku, String talent_star, String naiyou) throws Exception {
		// TODO Auto-generated method stub
		TalentDao dao = new TalentDao();
		TalentPickupDto tpDto = new TalentPickupDto();
		
		tpDto.setKubun(ku);
		tpDto.setNewstar_pickup_naiyou(naiyou);
		tpDto.setTalent_id(talent_star);
		
		dao.regPickup(tpDto);
	}

	public int getContent_Id(String talent_id) throws Exception {
		// TODO Auto-generated method stub
		TalentDao dao = new TalentDao();
		
		return dao.getContentId(talent_id);
	}    
}
